package com.linkstec.bookshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Category;
import com.linkstec.bookshop.pojo.Goods;
import com.linkstec.bookshop.service.CategoryService;
import com.linkstec.bookshop.service.GoodsService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.GoodsVo;

/**
 * 
 * @ClassName:  IndexController   首页的controller
 * @Description:TODO   
 * @author:  JGQiang
 * @date:   2018年11月23日 下午4:57:20
 */
@Controller
@RequestMapping("user")
public class IndexController {
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private GoodsService goodsService;

	/**
	 * 
	 * @Description:TODO 查询热门商品，商品分类，返回到首页。
	 * @author: JGQiang
	 * @date:   2018年11月23日 下午4:57:35    
	 * @param req
	 * @return
	 */
	@RequestMapping(value="index")
	public ModelAndView toIndex(HttpServletRequest req){
		ModelAndView mv =new ModelAndView();
		//查询商品分类
		List<Category> cList = null;
		
		//查询热门商品
		List<GoodsVo> hList = null;
		
		try {
			cList = categoryService.findCategory();
			hList = goodsService.findHot();
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getSession().setAttribute("cList", cList);
		mv.addObject("hList", hList);
		
		mv.setViewName("index");
		return mv;
	}
	
	/**
	 * 
	 * @Description:TODO 根据商品分类查询商品
	 * @author: JGQiang
	 * @date:   2018年11月23日 下午5:09:15    
	 * @param page
	 * @param cgId
	 * @return
	 */
	@RequestMapping(value="findGoodsByCategory")
	public ModelAndView findGoodsByCategory(int page,int cgId){
		ModelAndView mv = new ModelAndView();
		PageBean<GoodsVo> goodsBean = new PageBean<GoodsVo>();
			try {
				goodsBean =goodsService.findGoodsByCategory(cgId,page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("goodsBean", goodsBean);
		mv.addObject("cgId", cgId);
		mv.setViewName("category");
		return mv;
	}
	
	/**
	 * 
	 * @Description:TODO 模糊查询商品
	 * @author: JGQiang
	 * @date:   2018年11月23日 下午5:14:17    
	 * @return
	 */
	@RequestMapping(value ="searchGoods")
	public ModelAndView searchGoods(String condition,int page){
		ModelAndView mv = new ModelAndView();
		PageBean<GoodsVo> goodsBean = new PageBean<GoodsVo>();
		try {
			goodsBean = goodsService.findGoodsVague(page,condition);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mv.addObject("goodsBean", goodsBean);
		mv.addObject("condition", condition);
		mv.setViewName("searchProduct");
		return mv;
	}
	
	
}
